package com.ruoyi.utils.txy;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.Bucket;
import com.qcloud.cos.region.Region;
import com.ruoyi.common.core.domain.AjaxResult;

import java.util.List;

public class TxyOssDomainUtils {

    /**
     * 创建请求对象
     */
    private static COSClient createCli(String secretId, String secretKey) {
        // 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
        // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
        // 生成cos客户端
        return new COSClient(cred, clientConfig);
    }

    private static AjaxResult listBuckets(String secretId, String secretKey) {
        try {
            //获得请求客户端对象
            COSClient cosClient = createCli(secretId, secretKey);
            List<Bucket> buckets = cosClient.listBuckets();
            return AjaxResult.success("", buckets);
        } catch (Exception e) {
            return AjaxResult.error("查询腾讯云的桶错误："+e.getMessage());
        }
    }

}
